package com.eshop.eshop2021_v1.controller.before;

import com.eshop.eshop2021_v1.pojo.GoodType;
import com.eshop.eshop2021_v1.pojo.Goods;
import com.eshop.eshop2021_v1.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: 若若叶
 * @date: 2021/10/7 9:55 FileName: GoodsController Description:
 */
@Controller
public class GoodsController {
  @Autowired private GoodsService goodsService;

  @RequestMapping(value = {"/", "/index"})
  public String showGoods(Model model) {
    String view = goodsService.showGoods(model);
    return view;
  }

  // 根据商品类型Id查找所有该类型商品
  @GetMapping("/findGoodsByTypeId")
  public String findGoodsByTypeId(Integer typeId, HttpSession session, Model model) {
    List<Goods> list = goodsService.findGoodsByTypeId(typeId, session);
    model.addAttribute("goodList", list);
    List<Goods> list1 = goodsService.showadvertisementGoods(); // 获得广告商品的集合
    List<GoodType> list2 = goodsService.showGoodsType(); // 商品类型集合
    model.addAttribute("advertisementGoods", list1);
    model.addAttribute("goodTypes", list2);

    return "user/searchResult";
  }

  // 根据id获取商品详情
  @GetMapping("/goodsDetail")
  public String goodsDetail(Integer goodId, Model model) {
    String view = goodsService.findGoodByGoodId(goodId, model);
    return view;
  }
}
